import type {RouteRecordRaw} from 'vue-router';
import {createRouter, createWebHistory} from 'vue-router';
import type {App} from 'vue';
import {h} from 'vue';
import {createRouterGuards} from '@/router/guard';
import Layout from '../layout/index.vue';
import {
  ControlOutlined,
  DashboardOutlined,
  PieChartOutlined,
  ProfileOutlined,
  ProjectOutlined,
  CodeOutlined,
} from '@ant-design/icons-vue';

/**
 * meta: {
 *   title: 页面标题
 *   permissions: 页面权限
 *   icon: 图标
 *   hidden: 是否在菜单栏隐藏
 *   alwaysShow: 只有一个子菜单是否显示自身
 *   keepAlive: 是否缓存页面
 * }
 */
export const ErrorRoute: RouteRecordRaw = {
  path: '/:path(.*)*',
  name: 'ErrorPage',
  component: () => import('@/views/404.vue'),
  meta: {
    title: 'ErrorPage',
  },
};

export const ConstantRoute: RouteRecordRaw[] = [
  {
    path: '/login',
    name: 'Login',
    component: () => import('@/views/Login.vue'),
    meta: { title: '登陆' },
  },
  {
    path: '/',
    redirect: '/home',
  },
  {
    path: '/home',
    name: 'home',
    component: Layout,
    redirect: '/home/index',
    meta: {
      title: 'Dashboard',
      icon: () => h(DashboardOutlined),
    },
    children: [
      {
        path: 'index',
        name: 'Home',
        component: () => import('@/views/Home.vue'),
        meta: { title: '首页' },
      },
    ],
  },
  {
    path: '/redirect',
    name: 'redirect',
    component: Layout,
    meta: {
      title: 'Redirect',
    },
    children: [
      {
        path: '/redirect/:path(.*)',
        name: 'Redirect',
        component: () => import('@/views/redirect/index.vue'),
        meta: {
          title: 'Redirect',
        },
      },
    ],
  },
];

export const AuthRoute: RouteRecordRaw[] = [
  {
    path: '/system',
    name: 'system',
    component: Layout,
    meta: {
      title: '系统管理',
      icon: () => h(ControlOutlined),
      permissions: ['user:query', 'role:query', 'menu:query'],
    },
    redirect: '/system/user',
    children: [
      {
        path: '/system/user',
        name: 'UserPage',
        component: () => import('@/views/sys/UserPage.vue'),
        meta: { title: '用户管理', permissions: ['user:query'] },
      },
      {
        path: '/system/role',
        name: 'RolePage',
        component: () => import('@/views/sys/RolePage.vue'),
        meta: { title: '角色管理', permissions: ['role:query'] },
      },
      {
        path: '/system/menu',
        name: 'MenuPage',
        component: () => import('@/views/sys/MenuPage.vue'),
        meta: { title: '菜单管理', permissions: ['menu:query'] },
      },
    ],
  },

  {
    path: '/account',
    name: 'account',
    component: Layout,
    meta: {
      title: '账号管理',
      icon: () => h(ControlOutlined),
      permissions: ['organization:query', 'user_group:query', 'sso_user:query'],
    },
    redirect: '/account/organization',
    children: [
      {
        path: '/account/organization',
        name: 'Organization',
        component: () => import('@/views/sso/Organization.vue'),
        meta: { title: '组织管理', permissions: ['organization:query'] },
      },
      {
        path: '/account/job',
        name: 'Job',
        component: () => import('@/views/sso/Job.vue'),
        meta: {  title: '职位管理', permissions: ['job:query']},
      },
      {
        path: '/account/user_group',
        name: 'UserGroup',
        component: () => import('@/views/sso/UserGroup.vue'),
        meta: {  title: '用户组管理', permissions: ['user_group:query']},
      },
      {
        path: '/account/user_group_detail',
        name: 'UserGroupDetail',
        component: () => import('@/views/sso/UserGroupDetail.vue'),
        meta: {
          hidden:true,
          title: '用户组管理详细',
          permissions: ['user_group:query']
        },
      },
      {
        path: '/account/sso_user',
        name: 'SsoUser',
        component: () => import('@/views/sso/SsoUser.vue'),
        meta: { title: '用户管理', permissions: ['sso_user:query'] },
      },
    ],
  },


  {
    path: '/app',
    name: 'app',
    component: Layout,
    meta: {
      title: '应用管理',
      icon: () => h(ControlOutlined),
      permissions: ['app:query', 'app:group', 'app:create'],
    },
    redirect: '/app/group',
    children: [
      {
        path: '/app/group',
        name: 'AppGroup',
        component: () => import('@/views/app/AppGroup.vue'),
        meta: {  title: '应用分组', permissions: ['app:group']},
      },
      {
        path: '/app/create',
        name: 'AppCreate',
        component: () => import('@/views/app/AppCreate.vue'),
        meta: { title: '创建应用', permissions: ['app:create'] },
      },
      {
        path: '/app/oidcCreate',
        name: 'OidcCreate',
        component: () => import('@/views/app/OidcCreate.vue'),
        meta: {
          hidden:true,
          title: 'OIDC应用创建',
          permissions: ['app:create']
        },
      },
      {
        path: '/app/jwtCreate',
        name: 'JwtCreate',
        component: () => import('@/views/app/JwtCreate.vue'),
        meta: {
          hidden:true,
          title: 'Jwt应用创建',
          permissions: ['app:create']
        },
      },
      {
        path: '/app/list',
        name: 'AppList',
        component: () => import('@/views/app/AppList.vue'),
        meta: { title: '应用列表', permissions: ['app:query'] },
      },
    ],
  },


  {
    path: '/res',
    name: 'res',
    component: Layout,
    meta: {
      title: '资源管理',
      icon: () => h(ControlOutlined),
      permissions: ['res:query'],
    },
    redirect: '/res/file',
    children: [
      {
        path: '/res/file',
        name: 'Files',
        component: () => import('@/views/res/Files.vue'),
        meta: {  title: '资源', permissions: ['res:query']},
      },
    ],
  },

  {
    path: '/demo',
    name: 'demo',
    component: Layout,
    meta: {
      title: '样例',
      icon: () => h(PieChartOutlined),
    },
    children: [
      {
        path: 'chart',
        name: `ChartDemo`,
        meta: {
          title: 'Echart样例',
        },
        component: () => import('@/views/demo/ChartDemo.vue'),
      },
      {
        path: 'excel',
        name: `ExcelDemo`,
        meta: {
          title: 'Excel样例',
        },
        component: () => import('@/views/demo/ExcelDemo.vue'),
      },
    ],
  },
  {
    path: '/log',
    name: 'log',
    component: Layout,
    meta: {
      icon: () => h(ProfileOutlined),
      permissions: ['*'],
    },
    children: [
      {
        path: 'operation',
        name: `OperLogPage`,
        meta: {
          title: '操作日志',
        },
        component: () => import('@/views/log/OperLogPage.vue'),
      },
    ],
  },
  {
    path: '/code',
    name: 'code',
    component: Layout,
    meta: {
      icon: () => h(CodeOutlined),
      permissions: ['*'],
    },
    children: [
      {
        path: 'index',
        name: `CodeGen`,
        meta: {
          title: '代码生成',
        },
        component: () => import('@/views/code/index.vue'),
      },
    ],
  },
  {
    path: '/about',
    name: 'about',
    component: Layout,
    meta: {
      icon: () => h(ProjectOutlined),
      permissions: ['*'],
    },
    children: [
      {
        path: 'index',
        name: `about_index`,
        meta: {
          title: '关于',
        },
        component: () => import('@/views/about/index.vue'),
      },
    ],
  },
];

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: ConstantRoute,
});

export function setupRouter(app: App) {
  app.use(router);
  // 创建路由守卫
  createRouterGuards(router);
}

export default router;
